<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateClassifyPermissionsTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('classify_permissions')) { //如果不存在则建表
            Schema::create('classify_permissions', function (Blueprint $table) {
                $this->create_table($table);
            });
        } else { //否则增量添加字段
            Schema::table('classify_permissions', function (Blueprint $table) {
                $this->create_table($table);
            });
        }
        DB::statement("alter table `classify_permissions` comment'权限分类表'");
    }
    public function create_table($table)
    {
        $table->engine = 'InnoDB';
       
        if (!Schema::hasColumn('classify_permissions', 'id')) {
            $table->increments('id');
        }
        if (!Schema::hasColumn('classify_permissions', 'title')) {
            $table->string('title', 32)->default('')->comment('分类标题');
        }
        if (!Schema::hasColumn('classify_permissions', 'parent_id')) {
            $table->integer('parent_id')->default(0)->comment('父级id');
        }

        if (!Schema::hasColumn('classify_permissions', 'rank')) {
            $table->tinyInteger('rank')->default(0)->comment('排序');
        } 

        if (!Schema::hasColumn('classify_permissions', 'created_at')) {
            $table->timestamps();
        }
        if (!Schema::hasColumn('classify_permissions', 'deleted_at')) {
            $table->softDeletes();
        }
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Schema::dropIfExists('classify_permissions');
    }
}
